﻿
// MFCApplication2View.cpp: CMFCApplication2View 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MFCApplication2.h"
#endif

#include "MFCApplication2Doc.h"
#include "MFCApplication2View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCApplication2View

IMPLEMENT_DYNCREATE(CMFCApplication2View, CView)

BEGIN_MESSAGE_MAP(CMFCApplication2View, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_COMMAND(ID_SPEED_UP, &CMFCApplication2View::OnSpeedUp)
	ON_COMMAND(ID_SLOW_DOWN, &CMFCApplication2View::OnSlowDown)
	ON_COMMAND(ID_CHECK_SPEED, &CMFCApplication2View::OnCheckSpeed)
	ON_COMMAND(ID_CONTINUE, &CMFCApplication2View::OnContinue)
	ON_COMMAND(ID_STOP, &CMFCApplication2View::OnStop)
END_MESSAGE_MAP()

// CMFCApplication2View 构造/析构

CMFCApplication2View::CMFCApplication2View() noexcept
{
	//小球初始坐标，移动像素间距，和记录timer间隔的变量
	x = y = 0;
	movx = movy = 10;
	timeC = 500;
}

CMFCApplication2View::~CMFCApplication2View()
{
}

BOOL CMFCApplication2View::PreCreateWindow(CREATESTRUCT& cs)
{
	//在MainFrm里修改，使得主框架大小固定，在视图区就不用修改了

	return CView::PreCreateWindow(cs);
}

// CMFCApplication2View 绘图

void CMFCApplication2View::OnDraw(CDC* pDC)
{
	CMFCApplication2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	//异或画笔，填充黑色
	pDC->SetROP2(R2_XORPEN);
	//画直径为15的圆
	pDC->Ellipse(x, y, x + 15, y + 15);
}


// CMFCApplication2View 打印

BOOL CMFCApplication2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CMFCApplication2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CMFCApplication2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CMFCApplication2View 诊断

#ifdef _DEBUG
void CMFCApplication2View::AssertValid() const
{
	CView::AssertValid();
}

void CMFCApplication2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMFCApplication2Doc* CMFCApplication2View::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCApplication2Doc)));
	return (CMFCApplication2Doc*)m_pDocument;
}
#endif //_DEBUG


// CMFCApplication2View 消息处理程序


int CMFCApplication2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	//设置计时器，ID为1，间隔为timeC(单位：ms)
	SetTimer(1, timeC, NULL);
	return 0;
}


void CMFCApplication2View::OnDestroy()
{
	CView::OnDestroy();
	//销毁计时器
	KillTimer(1);
}


void CMFCApplication2View::OnTimer(UINT_PTR nIDEvent)
{
	//计时器1响应
	if (nIDEvent == 1) {
		CDC* pDC = GetDC();

		pDC->SetROP2(R2_XORPEN);
		//异或画笔，在原位置画圆，异或后擦除原有圆
		pDC->Ellipse(x, y, x + 15, y + 15);
		CRect rect;
		//读取窗口客户区大小
		GetClientRect(&rect);
		//若小球坐标将要在客户区之外，则更改移动方向
		if (y<0 || y > rect.bottom - 15) movy = -movy;
		if (x<0 || x > rect.right - 15) movx = -movx;
		//小球平移
		x += movx;
		y += movy;
		//画出新圆
		pDC->Ellipse(x, y, x + 15, y + 15);
		ReleaseDC(pDC);
	}
	CView::OnTimer(nIDEvent);
}


void CMFCApplication2View::OnSpeedUp()
{
	//菜单项中速度增加
	//若时间间隔timeC仍在80ms之上则可以减少其值，实现加速，否则会提示达到最大速度
	if (timeC >= 80)
		timeC -= 70;
	else
		MessageBox(L"已达到最大速度！(level 10)");
	//通过重新set改变时间间隔
	SetTimer(1, timeC, NULL);
}


void CMFCApplication2View::OnSlowDown()
{
	//菜单项中速度增加
	//若时间间隔timeC仍在570ms之下则可以增加其值，实现减速，否则会提示达到最小速度
	if (timeC <= 570)
		timeC += 70;
	else
		MessageBox(L"已达到最小速度！(level 1)");
	//通过重新set改变时间间隔
	SetTimer(1, timeC, NULL);
}


void CMFCApplication2View::OnCheckSpeed()
{
	//查看当前速度
	//初始速度为level 3，间隔500ms；最低level 1，间隔640ms；最高level 10，间隔10ms；
	//各等级相差70ms
	int level = 3 - (timeC - 500) / 70;
	CString str;
	str.Format(L"当前速度等级level = %d (level 1 ~ 10)", level);
	MessageBox(str);
}


void CMFCApplication2View::OnContinue()
{
	//小球继续，重新设置timer，timeC记录暂停前的间隔
	SetTimer(1, timeC, NULL);
}


void CMFCApplication2View::OnStop()
{
	//小球暂停，直接销毁timer可以暂停
	KillTimer(1);
}
